package com.jiangxfei.mymvp.app.home.mvp;

import android.arch.lifecycle.LifecycleOwner;
import android.content.Context;
import android.support.annotation.NonNull;

import com.jiangxfei.mymvp.app.home.adapter.RecyLoadAdapter;
import com.jiangxfei.mymvp.app.home.bean.User;
import com.jiangxfei.mymvp.base.BasePersenter;
import com.jiangxfei.mymvp.base.IPresenter;
import com.jiangxfei.mymvp.di.ActivityScope;
import com.jiangxfei.mymvp.net.ErrorHandleObserver;
import com.jiangxfei.mymvp.net.RetryWithDelay;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author: JiangXFei
 * @date: 2018/1/9 0009
 * @content:
 */

@ActivityScope
public class HomePresenter extends BasePersenter<HomeConstract.Model, HomeConstract.View> implements IPresenter {

    @Inject
    Context mContext;
    @Inject
    List<User> mUsers;
    @Inject
    RecyLoadAdapter adapter;
    int lastUserId = 1;    //请求最后一次的用户ID
    boolean isFrist = true;  //是否第一次请求

    @Inject
    public HomePresenter(HomeConstract.Model model, HomeConstract.View view) {
        super(model, view);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
        requestUser(true);
    }

    public void requestUser(final boolean isRefresh) {

        //是否是刷新事件
        if (isRefresh) {
            lastUserId = 1;
        }

        boolean isEvictCache = isRefresh; //是否使用缓存
        //判断是否是第一次请求
        if (isRefresh && isFrist) {
            isEvictCache = false;
            isFrist = false;
        }

        mModel.loadUser(lastUserId, isEvictCache)
                .subscribeOn(Schedulers.io())
//                遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .retryWhen(new RetryWithDelay(3, 2))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        //显示进度条
                        mView.showLoading();

                    }
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏进度条
                    mView.hideLoading();
                })
                .subscribe(new ErrorHandleObserver<List<User>>() {
                    @Override
                    public void onNext(List<User> userList) {
                        lastUserId = userList.get(userList.size() - 1).getId();
                        if (isRefresh) {
                            mUsers.clear();
                        }
                        mUsers.addAll(userList);
                        adapter.notifyDataSetChanged();
                    }
                });
    }

//    public void test() {
//        HomeConstract.TestParam testParam = mView.getTestParam();
//        mModel.test(testParam)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ErrorHandleObserver<Object>() {
//                    @Override
//                    public void onNext(Object o) {
//                        mView.testOnSuccess((String) o);
//                    }
//                });
//    }
//
//    public  void Login(){
//        HomeConstract.LoginParam loginParam = mView.getLoginParam();
//        mModel.login(loginParam)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ErrorHandleObserver<Object>() {
//                    @Override
//                    public void onNext(Object o) {
//
//                    }
//                });
//
//    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mUsers = null;
        adapter = null;
        mContext = null;
    }
}
